package com.example.tliaswebmanagement.controller;

import com.example.tliaswebmanagement.anno.Log;
import com.example.tliaswebmanagement.pojo.Dept;
import com.example.tliaswebmanagement.pojo.Result;
import com.example.tliaswebmanagement.service.DeptService;
import com.example.tliaswebmanagement.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RequestMapping("/depts")
//部门管理控制器
@RestController
public class DeptController {
    //private static final Logger log =  LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;
    private String id;

    //@RequestMapping(value = "/depts",method = RequestMethod.GET)//指定请求方式为GET
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据");
        //调用service查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门:{}", id);
        deptService.delete(id);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("新增部门{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result select(@PathVariable Integer id){
        log.info("根据id查询部门:{}",id);
        Dept dept = deptService.select(id);
//        List<Dept> deptList = deptService.select(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("根据id修改部门{}",dept);
        deptService.update(dept);
        Result select = select(dept.getId());
        return Result.success(select);
    }

}